package cn.cyh.org.controller;

import cn.cyh.basic.uitls.AJaxResult;
import cn.cyh.basic.uitls.PageList;
import cn.cyh.org.domain.Employee;
import cn.cyh.org.query.EmployeeQuery;
import cn.cyh.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    // 新增和修改 put
    @PutMapping
    public AJaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){ // 如果没有则为添加
                employeeService.save(employee);
            } else{ // 否则是修改
                employeeService.update(employee);
            }
            return AJaxResult.myself();

        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }
    // 删除 delete
    @DeleteMapping("/{id}")
    public AJaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AJaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }

    // 查询 get
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
       return employeeService.loadById(id);
    }

    // 查询所有部门
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.loadAll();
    }

    // 分页查询
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        PageList<Employee> employeePageList = employeeService.queryPage(query);
        return employeePageList;
    }
    // 批量删除
    @PatchMapping
    public AJaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return AJaxResult.myself();
        } catch (Exception e){
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }

}
